package com.weijianhuawen.wjblog.admin.event.subscriber;

import com.weijianhuawen.wjblog.admin.event.DeleteArticleEvent;
import com.weijianhuawen.wjblog.admin.service.AdminStatisticsService;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleCategoryRelDo;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.weijianhuawen.wjblog.search.index.ArticleIndex;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.index.Term;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-08 13:28
 */

@Component
@Slf4j
public class DeleteArticleStatisticsSubscriber implements ApplicationListener<DeleteArticleEvent> {
    @Autowired
    private AdminStatisticsService adminStatisticsService;
    @Override
    public void onApplicationEvent(DeleteArticleEvent event) {
        // 消费 删除文章 事件
        Long categoryId = event.getBindCategoryId();
        List<Long> tagIds = event.getBindTagIdList();
        // 获取当前线程名称
        String threadName = Thread.currentThread().getName();

        log.info("【删除文章事件订阅者 统计】 线程：{}开始处理订阅者行为！", threadName );
        log.info("【删除发布事件订阅者 统计】 开始消费！处理分类id：{}", categoryId);
        // 更新对应分类id 下的文章数量
        adminStatisticsService.statisticsOneCategoryArticleTotal(categoryId);
        // 更新对应标签下的文章数量
        if (!CollectionUtils.isEmpty(tagIds)) {
            adminStatisticsService.statisticsSomeTagsArticleTotal(tagIds);
        }
        log.info("【删除发布事件订阅者 统计】 完成！");
    }
}
